'''
    Djiscography: A Django-based Discography generator
    Copyright 2010 Martin Paul Eve

    This file is part of Djiscography.

    Djiscography is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    Djiscography is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with Djiscography.  If not, see <http://www.gnu.org/licenses/>.

'''

from Djiscography.models import *
from django.shortcuts import render_to_response, get_object_or_404

def discography(request):
    sections = ReleaseType.objects.all().order_by('display_order')
    releases = Release.objects.all().order_by('-year','order','name')
    tracks = Track.objects.all().order_by('track_number')
    editions = Edition.objects.all().order_by('-year','release__name','catalogue_number')
    lyrics = Lyric.objects.all()
    
    section_list = []
    
    for section in sections:
            spliced_releases = releases.filter(type=section)
            
            release_list = []
            
            for release in spliced_releases:
                spliced_editions = editions.filter(release=release)
                
                edition_list = []
                
                for edition in spliced_editions:
                    spliced_tracks = tracks.filter(edition=edition)
                    
                    track_list = []
                    
                    for track in spliced_tracks:
                        
                        track_dict = {'track': track}
                        track_dict['lyric'] = ''
                        
                        for lyric in lyrics:
                            if lyric.track == track.name:
                                track_dict['lyric'] = lyric
                    
                        track_list.append(track_dict)
                        
                    images = Image.objects.filter(edition=edition).order_by('display_name')
                        
                    edition_dict = {'edition': edition, 'tracks': track_list, 'images': images}
                    edition_list.append(edition_dict)
                
                release_dict = {'release': release, 'editions': edition_list}
                release_list.append(release_dict)
                    
            
            section_dict = {'section': section, 'releases': release_list}
            section_list.append(section_dict)
            
    return render_to_response('discography.html', {'sections': section_list})

def lyrics(request, track_name):
    lyric = get_object_or_404(Lyric, track=track_name)
    
    return render_to_response('lyrics.html', {'lyric': lyric.lyrics,
                                              'comments': lyric.comments})    
